import  pymysql

headers = {
    'user-agent': 'Mozilla/5.0 (iPhone; CPU iPhone OS 11_0 like Mac OS X) AppleWebKit/604.1.38 (KHTML, like Gecko) Version/11.0 Mobile/15A372 Safari/604.1'
}

db = pymysql.connect("119.23.229.156", "gwrUser", "Gwr123456!", "orderMeal",cursorclass = pymysql.cursors.DictCursor)
cursor = db.cursor()



def excSql(sql):
    try:
        cursor.execute(sql)
        db.commit()
    except Exception as e:
        print(e)
        db.rollback()

def excSqls(sqls):
    try:
        for sql in sqls:
            cursor.execute(sql)
        db.commit()
    except Exception as e:
        print(e)
        db.rollback()

def get_insert_sql(dataDic,name):
    key_list = []
    value_list = []
    for data in dataDic:
        value = dataDic.get(data)
        if not value:
            value = ""
        key_list.append(str(data))
        value = str(value)
        value = value.replace("'", "\"")
        value_list.append("'"+str(value)+"'")
    sql_key = ",".join(key_list)
    sql_value = ",".join(value_list)
    sql = "insert into %s  (%s) values (%s)"%(name,sql_key,sql_value)
    return  sql

def insert_list(dataList,name):
    all_sql = []
    for data in dataList:
        insql = get_insert_sql(data,name)
        all_sql.append(insql)
    excSqls(all_sql)




def closeDB():
    db.close()